package org.dfl.plugin.views.actions;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.dfl.plugin.dialogs.DialogsManager;
import org.dfl.plugin.views.debug.model.HistoryColTreeElem;
import org.dfl.plugin.views.debug.model.HistoryTreeElem;
import org.dfl.plugin.views.debug.model.MapKeyTreeElem;
import org.dfl.plugin.views.debug.model.TokenTreeElem;
import org.dfl.plugin.views.debug.model.TreeElem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.actions.ActionDelegate;

/**
 * action that provide copying a content to data objects. It's used in a
 * HistoriesTreeView and TokensTreeView (see mapping in plugin.xml file)
 * 
 * @author Piotr Wlodarczyk
 * 
 */
public class CopyContentAction extends ActionDelegate {
	private static Set<TreeElem> copyElems = new HashSet<TreeElem>();
	private IStructuredSelection selection = StructuredSelection.EMPTY;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
	 *      org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(IAction action, ISelection sel) {
		if (sel instanceof IStructuredSelection)
			selection = (IStructuredSelection) sel;
		else
			selection = StructuredSelection.EMPTY;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.actions.ActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	@SuppressWarnings("unchecked")
	public void run(IAction action) {
		if (selection.isEmpty())
			return;

		copyElems.clear();
		Iterator iter = selection.iterator();
		Set<TreeElem> toCopy = new HashSet<TreeElem>();
		while (iter.hasNext()) {
			Object elem = iter.next();
			if (elem instanceof TreeElem) {
				toCopy.add((TreeElem) elem);
			}
		}
		for (TreeElem elem : toCopy) {
			TreeElem e = elem;
			boolean found = false;
			do {
				if (e != elem && toCopy.contains(e)) {
					found = true;
				} else {
					e = e.getParent();
				}
			} while (!found && e != null);
			if (!found) {
				if (elem instanceof HistoryColTreeElem) {
					copyElems
							.add((HistoryColTreeElem) ((HistoryColTreeElem) elem)
									.getCopyFullCopy());
				} else {
					copyElems.add(elem.getCopy());
				}
			}
		}

		// check if all elements are the same type
		if (!copyElems.isEmpty()) {
			TreeElem first = copyElems.iterator().next();
			Class clazz = first.getClass();
			for (TreeElem elem : copyElems) {
				if (elem != first && !elem.getClass().equals(clazz)) {
					DialogsManager
							.openErrorDialog("Copy only elements of the same type");
					toCopy.clear();
					System.setProperty("org.dfl.plugin.actions.paste.active",
							"false");
					return;
				}
			}
			System.setProperty("org.dfl.plugin.actions.paste.active", "true");
			System.setProperty("org.dfl.plugin.actions.paste.history.active",
					clazz.equals(HistoryTreeElem.class) ? "true" : "false");
			System.setProperty("org.dfl.plugin.actions.paste.token.active",
					clazz.equals(TokenTreeElem.class) ? "true" : "false");
			System.setProperty(
					"org.dfl.plugin.actions.paste.historycol.active", clazz
							.equals(HistoryColTreeElem.class) ? "true"
							: "false");
			System.setProperty("org.dfl.plugin.actions.paste.mapkey.active",
					clazz.equals(MapKeyTreeElem.class) ? "true" : "false");
			// System.setProperty("org.dfl.plugin.actions.paste.collection.active",
			// clazz.equals(CollectionTreeElem.class)?"true":"false");
			// System.setProperty("org.dfl.plugin.actions.paste.map.active",
			// clazz.equals(MapTreeElem.class)?"true":"false");
			// System.setProperty("org.dfl.plugin.actions.paste.boolean.active",
			// clazz.equals(BooleanTreeElem.class)?"true":"false");
			// System.setProperty("org.dfl.plugin.actions.paste.binary.active",
			// clazz.equals(BinaryTreeElem.class)?"true":"false");
			// System.setProperty("org.dfl.plugin.actions.paste.string.active",
			// clazz.equals(StringTreeElem.class)?"true":"false");
		} else {
			System.setProperty("org.dfl.plugin.actions.paste.active", "false");
		}

	}

	public static Set<TreeElem> getCopyElems() {
		return copyElems;
	}
}
